package cn.dujc.superkid.activities;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.text.Spanned;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.Fragement_testAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ChannelEntity;
import cn.dujc.superkid.bean.GoodQuestionResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.fragment.GoodFragment;
import cn.dujc.superkid.utils.Preferences;

// TODO 接口问题：暂无数据时 标题也不会显示
public class GoodQueActivity extends BaseActivity {

    private TabLayout tabLayout = null;
    private ViewPager vp_pager;
    private List<String> titles = new ArrayList<>();
    private GoodQuestionResult data;
    @Override
    public int getViewId() {
        return R.layout.activity_good_que;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        setTitle("好题本");

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        final QMap.Builder map = QMap.build();
        final ChannelEntity channel = Preferences.getInstance().getChannel(mActivity);
        if (channel != null) map.put("channel", channel.getChannel_id());
        HttpHelper.getUserCenter().memberGoodQuestion(map
                .put("user_id", loginResult.getUid())
                .put("logincode", loginResult.getLogincode())
                .put("num", 1)
                .put("page", 0)
                .create()).enqueue(new ResultCallBack<Result<GoodQuestionResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<GoodQuestionResult> body) {
                data = body.getData();
                List<Fragment> fragments = new ArrayList<>();
                if (data != null) {
                    for (int i = 0; i < data.getCategory().size(); i++) {
                        titles.add(data.getCategory().get(i).getName());
                        tabLayout.addTab(tabLayout.newTab().setText(titles.get(i)));
                        final GoodFragment goodFragment = new GoodFragment();
                        final Bundle args = new Bundle();
                        args.putString("str",data.getCategory().get(i).getId());
                        goodFragment.setArguments(args);
                        fragments.add(goodFragment);
                    }
                }
                Fragement_testAdapter fragementAdapter = new Fragement_testAdapter(getSupportFragmentManager(), fragments, titles);
                vp_pager.setAdapter(fragementAdapter);
                tabLayout.setupWithViewPager(vp_pager);
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                findViewById(R.id.good_empty).setVisibility(View.VISIBLE);
                tabLayout.setVisibility(View.GONE);
            }
        });
    }
    private void initViews() {
        tabLayout = findViewById(R.id.tablayout);
        vp_pager = findViewById(R.id.tab_viewpager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
}
